package com.zlj.user.web.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zlj.user.domain.User;
import com.zlj.user.service.UserException;
import com.zlj.user.service.UserService;

import cn.itcast.commons.CommonUtils;

/**
 * User 的servlet层
 * @author bashen
 *
 */
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");  //设置请求编码(POST)
		response.setContentType("text/html;charset=utf-8");  //设置响应编码
		
		UserService userService = new UserService();
		
		/*
		 * 1、封装表单数据到User对象中
		 */
		User user = CommonUtils.toBean(request.getParameterMap(), User.class);
		
		Map<String, String> errors = new HashMap<String, String>(); 
		
		//对用户名进行校验
		String username = user.getUsername();  //获取表单中的username数据
		if(username == null || username.trim().isEmpty()) {
			errors.put("username", "用户名不能为空！");
		} else if(username.length() < 2 || username.length() > 15) {
			errors.put("username", "用户名长度必须为2~15之间");
		}
		
		//对密码进行校验
		String password = user.getPassword();
		if(password == null || password.trim().isEmpty()) {
			errors.put("password", "密码不能为空！");
			request.setAttribute("errors", "密码不能为空！");
		} else if(password.length() < 2 || username.length() > 15) {
			errors.put("password", "密码长度必须为3~15之间");
		}
		
		//对验证码进行校验
		String sessionVCode = (String) request.getSession().getAttribute("session_vcode");
		String verifyCode = user.getVerifyCode();
		if(verifyCode == null || verifyCode.trim().isEmpty()) {
			errors.put("verifyCode", "验证码不能为空！");
		} else if(verifyCode.length() != 4) {
			errors.put("verifyCode", "验证码长度必须为4");
		}else if(!verifyCode.equalsIgnoreCase(sessionVCode)) {
			errors.put("verifyCode", "验证码错误");
		}
		
		/*
		 * 判断map是否为null,如果不为null，说明存在错误
		 */
		if(errors != null && errors.size() > 0 ) {
			//保存User到Request域中，为了回显
			request.setAttribute("user", user);
			//保存errors到Request域中
			request.setAttribute("errors", errors);
			//转发到regist.jsp页面中
			request.getRequestDispatcher("/user/regist.jsp").forward(request, response);
			return;
		}
		
		/*
		 * 2、调用UserService的regist()方法，传递user过去
		 * 3、得到异常，获取异常信息，保存到Request域中，转发到regist.jsp中显示
		 * 4、没有异常，输出注册成功
		 */
		try {
			userService.regist(user);
			response.getWriter().println("<h1>注册成功</h1><a href='"+request.getContextPath() + "/user/login.jsp" +"'>点击这里跳转登录页面</a>");
		} catch (UserException e) {
			//获取异常信息，保存到request域中
			request.setAttribute("msg", e.getMessage());
			
			//保存表单数据到Request域中，为了回显
			request.setAttribute("user", user);
			
			//转发到regist.jsp页面中
			request.getRequestDispatcher("/user/regist.jsp").forward(request, response);
		}
	}
}
